package mSocket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * @author Ajie
 * @date 2023/1/29
 * @function
 */
public class UDPMulticastSocket_Server {
    public static void main(String[] args) throws IOException {
        // Create a multicast socket on a specific port
        MulticastSocket socket = new MulticastSocket(5555);

        // Specify the multicast group to join
//        InetAddress group = InetAddress.getByName("230.0.0.1");//Can't assign requested address
//        InetAddress group = InetAddress.getLocalHost();
        InetAddress group = InetAddress.getByName("230.0.0.5");//
        socket.joinGroup(group);

        // Prepare a buffer to receive the packet
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        System.out.println("UDPMulticastSocket_Server:main:start receive data");
        // Receive the packet
        socket.receive(packet);

        // Print the message
        System.out.println(new String(buffer));

        // Leave the multicast group
        socket.leaveGroup(group);

        // Close the socket
        socket.close();
    }


}